﻿Imports System.Collections.Generic
Imports System.Security.Claims
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.OleDb
Imports System.Data.Entity.Validation

Public Class sms_comments1
    Inherits System.Web.UI.Page


    'Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init 
    '    If (Not IsPostBack) Then
    '        Me.bindgrid()
    '    End If
    'End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not Page.IsPostBack) Then
           bindgrid()

        End If
    End Sub



    Private Function Disable()
        txt_author.Enabled = False
        txt_comment.Enabled = False
        ddl_moneydata.Enabled = False
        txt_title.Enabled = False
        txt_author.Enabled = False
        RadioButton1.Enabled = False
        RadioButton2.Enabled = False
        RadioButton3.Enabled = False
        RadioButton4.Enabled = False
        RadioButton5.Enabled = False
    End Function

    Private Sub Enable()
        txt_author.Enabled = True
        txt_comment.Enabled = True
        ddl_moneydata.Enabled = True
        txt_title.Enabled = True
        txt_author.Enabled = True
        RadioButton1.Enabled = True
        RadioButton2.Enabled = True
        RadioButton3.Enabled = True
        RadioButton4.Enabled = True
        RadioButton5.Enabled = True
    End Sub

    Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
        Dim id As Integer
        id = Convert.ToInt32(GridView1.DataKeys(e.NewEditIndex).Value)
        hdn_id.Value = id



        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
        Dim SqlString As String = "select * from sms_comments where ID =  @ID"

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)


        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@ID", id)

        oleDbConn.Open()

        Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim ds As DataSet = New DataSet()
        sda.Fill(ds)


        Dim dr As DataRow = ds.Tables(0).Rows(0)

        txt_author.Text = dr("Author").ToString()
        txt_comment.Text = dr("Comment").ToString()
        txt_title.Text = dr("Title").ToString()
        ddl_moneydata.SelectedValue = dr("sms_moneydata_fkId").ToString()
        If (dr("rating").ToString() = "1") Then

            RadioButton1.Checked = True
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False

        ElseIf (dr("rating").ToString() = "2") Then
            RadioButton2.Checked = True
            RadioButton1.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False
        ElseIf (dr("rating").ToString() = "3") Then
            RadioButton3.Checked = True
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False
        ElseIf (dr("rating").ToString() = "4") Then
            RadioButton4.Checked = True
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton5.Checked = False
        ElseIf (dr("rating").ToString() = "5") Then
            RadioButton5.Checked = True
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
        Else
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False
        End If

        Disable()
        Button3.Text = "Edit"



    End Sub




    Protected Sub Button3_Click(sender As Object, e As EventArgs)
        Try
            If Button3.Text = "Edit" Then
                Enable()
                Button3.Text = "Save"
            ElseIf Button3.Text = "Save" Then

                If (txt_author.Text <> "" And txt_comment.Text <> "") Then

                    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
                    Dim SqlString As String = "SELECT * FROM sms_register  where username=@username"

                    Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@username", Session("UserName"))
                    oleDbConn.Open()
                    Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
                    Dim ds As DataSet = New DataSet()
                    sda.Fill(ds)


                    Dim oleDbConn1 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
                    Dim SqlString1 As String = "update sms_comments set Comment=@comment, Author=@author, Title=@title, sms_moneydata_fkId=@moneytype,  rating=@f5, sms_username_fkid=@username where  ID =  @ID"

                    Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString1, oleDbConn1)


                    cmd1.CommandType = CommandType.Text
                    cmd1.Parameters.AddWithValue("@comment", txt_comment.Text)
                    cmd1.Parameters.AddWithValue("@author", txt_author.Text)
                    cmd1.Parameters.AddWithValue("@title", txt_title.Text)
                    cmd1.Parameters.AddWithValue("@moneytype", ddl_moneydata.SelectedValue)
                    If RadioButton1.Checked = True Then
                        cmd1.Parameters.AddWithValue("@f5", 1)
                    ElseIf RadioButton2.Checked = True Then
                        cmd1.Parameters.AddWithValue("@f5", 2)
                    ElseIf RadioButton3.Checked = True Then
                        cmd1.Parameters.AddWithValue("@f5", 3)
                    ElseIf RadioButton4.Checked = True Then
                        cmd1.Parameters.AddWithValue("@f5", 4)
                    Else
                        cmd1.Parameters.AddWithValue("@f5", 5)

                    End If
                    cmd1.Parameters.AddWithValue("@username", ds.Tables(0).Rows(0)("ID").ToString())
                    cmd1.Parameters.AddWithValue("@ID", hdn_id.Value)
                    oleDbConn1.Open()
                    cmd1.ExecuteNonQuery()

                    clear()
                    Button3.Text = "Edit"
                    Disable()
                    bindgrid()
                   

                    txtTest.Value = "I am there"



                End If

            End If

        Catch ex As DbEntityValidationException

        End Try



    End Sub
    Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)

        GridView1.DataBind()

    End Sub
    Protected Sub btncancel_Click(sender As Object, e As EventArgs)
        Button3.Text = "Edit"
        Disable()


    End Sub

    Private Sub bindgrid()
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "SELECT sms_comments.ID,sms_comments.Author, sms_comments.Comment, sms_comments.Title, sms_comments.rating, sms_moneydata.Company_Name AS sms_moneydata_fkId, sms_register.username AS sms_username_fkid FROM   ((sms_comments INNER JOIN sms_moneydata ON sms_comments.sms_moneydata_fkId = sms_moneydata.ID) INNER JOIN sms_register ON sms_comments.sms_username_fkid = sms_register.ID) ORDER BY sms_comments.ID DESC"

            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim ds As DataSet = New DataSet()
            sda.Fill(ds)

            GridView1.DataSource = ds
            GridView1.DataBind()



        Catch ex As DbEntityValidationException

        End Try

    End Sub

    Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
        Try
            Dim id As Integer
            id = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "delete  FROM sms_comments where ID=@ID"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@ID", id)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            Me.bindgrid()
            'MyUpdatePanel.Update()

        Catch ex As DbEntityValidationException

        End Try


    End Sub
    Protected Sub clear()

        txt_author.Text = ""
        txt_comment.Text = ""
        txt_title.Text = ""
        'ddl_moneydata.SelectedValue =""
        RadioButton1.Checked = False
        RadioButton2.Checked = False
        RadioButton3.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
    End Sub

    Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)

        GridView1.PageIndex = e.NewPageIndex
    End Sub
    'Protected Sub SqlDataSource005_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource005.Selecting

    'End Sub

    Protected Sub GridView1_DataBound(sender As Object, e As EventArgs)
        txtTest.Value = "Im inside the grid"
    End Sub
End Class